package com.event.controller;


import com.alibaba.fastjson.JSON;
import com.event.pojo.Post;
import com.event.pojo.PostComment;
import com.event.pojo.User;
import com.event.service.PostCommentService;
import com.event.service.PostService;
import com.event.service.UserService;
import com.event.util.GetIp;
import com.event.util.SenInfoCheckUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 * 帖子一级评论 前端控制器
 * </p>
 *
 * @author 孤雏
 * @since 2021-08-09
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/comment")
@CrossOrigin(origins = "*",maxAge = 3600)
@Api(value = "用户帖子评论接口",tags = "用户帖子评论接口")
public class PostCommentController {

    @Autowired
    PostCommentService postCommentService;
    @Autowired
    PostService postService;
    @Autowired
    UserService userService;
    @Autowired
    RedisTemplate redisTemplate;
    @GetMapping("/selectComment")
    @ApiOperation(value = "按时间获取帖子评论接口,如果传入为空则为查询全部评论前8条",notes = "传值进来")
    public JSON selectComment(PostComment postComment){
        Map<Object, Object> map = new ConcurrentHashMap<>();
        String msg;
        List<PostComment> postComments = postCommentService.selectPostComment(postComment);
        for (PostComment postComment1 : postComments) {
            User user = new User();
            user.setUserid(postComment1.getUserId());
            List<User> users = userService.selectUser(user);
            postComment1.setRealname(users.get(0).getRealname());
            postComment1.setOpenname(users.get(0).getOpenname());
            postComment1.setAvatar(users.get(0).getAvatar());
        }
        if(postComments.size()>0) msg="200";
        else msg="500";
        map.put("code",0);
        map.put("msg",msg);
        map.put("count",postComments.size());
        map.put("data",postComments);
        JSON json = (JSON) JSON.toJSON(map);
        return json;
    }
    @GetMapping("/selectPostCommentByLike")
    @ApiOperation(value = "按点赞数获取帖子评论接口,如果传入为空则为查询全部",notes = "传值进来")
    public JSON selectPostCommentByLike(PostComment postComment){
        Map<Object, Object> map = new ConcurrentHashMap<>();
        String msg;
        List<PostComment> postComments = postCommentService.selectPostCommentByLike(postComment);
        for (PostComment postComment1 : postComments) {
            User user = new User();
            user.setUserid(postComment1.getUserId());
            List<User> users = userService.selectUser(user);
            postComment1.setRealname(users.get(0).getRealname());
            postComment1.setOpenname(users.get(0).getOpenname());
            postComment1.setAvatar(users.get(0).getAvatar());
        }
        if(postComments.size()>0) msg="200";
        else msg="500";
        map.put("code",0);
        map.put("msg",msg);
        map.put("count",postComments.size());
        map.put("data",postComments);
        JSON json = (JSON) JSON.toJSON(map);
        return json;
    }
    @GetMapping("/selectMessage")
    @ApiOperation(value = "筛选出用户的消息",notes = "传值进来")
    public JSON selectMessage(Integer userid){
        Map<Object, Object> map = new ConcurrentHashMap<>();
        PostComment postComment = new PostComment();
        List<PostComment> postComments = postCommentService.selectPostComment(postComment);
        List<PostComment> list = null;
        try {
            for (PostComment postComment1 : postComments) {
                User user = new User();
                user.setUserid(postComment1.getUserId());
                List<User> users = userService.selectUser(user);
                postComment1.setRealname(users.get(0).getRealname());
                postComment1.setOpenname(users.get(0).getOpenname());
                postComment1.setAvatar(users.get(0).getAvatar());
            }
            list = Collections.synchronizedList(new LinkedList<>());
            for (int i = 0; i < postComments.size(); i++) {
                if (postComments.get(i).getFaCommentId()!=null&& !postComments.get(i).getUserId().equals(userid)){
                    PostComment postComment1 = new PostComment();
                    postComment1.setCommentId(postComments.get(i).getFaCommentId());
                    List<PostComment> postComments1 = postCommentService.selectPostComment(postComment1);
                    if (postComments1.get(0).getUserId().equals(userid)){
                        list.add(postComments.get(i));
                    }
                }
                else if (postComments.get(i).getFaCommentId()==null&& !postComments.get(i).getUserId().equals(userid)){
                    Post post = new Post();
                    post.setPostId(postComments.get(i).getPostId());
                    List<Post> posts = postService.selectPost(post);
                    if (posts.get(0).getUserId().equals(userid)){
                        list.add(postComments.get(i));
                    }
                }
            }
            map.put("code",0);
            map.put("msg","200");
            map.put("count",list.size());
            map.put("data",list);
        } catch (Exception e) {
            map.put("code",0);
            map.put("msg","500");
            map.put("count",list.size());
        }
        JSON json = (JSON) JSON.toJSON(map);
        return json;
    }
//    @GetMapping("/selectSecondComment")
//    @ApiOperation(value = "筛选出所有对应的二级评论",notes = "传值进来")
//    public JSON selectSecondComment(Long CommentId){
//        LinkedHashMap<Object, Object> map = new LinkedHashMap<>();
//        ArrayList<PostComment> list = new ArrayList<>();
//        String msg;
//        PostComment postComment = new PostComment();
//        List<PostComment> postComments = postCommentService.selectPostComment(postComment);
//        try {
//            for (int i = 0; i < postComments.size(); i++) {
//                if (postComments.get(i).getFaCommentId()!=null){
//                    postComment.setCommentId(postComments.get(i).getFaCommentId());
//                    postComment = postCommentService.selectPostComment(postComment).get(0);
//                    while (postComment.getFaCommentId()!=null){
//                        PostComment postComment1 = new PostComment();
//                        postComment1.setCommentId(postComment.getFaCommentId());
//                        postComment = postCommentService.selectPostComment(postComment1).get(0);
//                    }
//                    if (postComment.getCommentId().equals(CommentId)){
//                        list.add(postComment);
//                    }
//                }
//            }
//            msg="200";
//        } catch (Exception e) {
//            e.printStackTrace();
//            msg="200";
//        }
//        map.put("code",0);
//        map.put("msg",msg);
//        map.put("count",list.size());
//        map.put("data",list);
//        JSON json = (JSON) JSON.toJSON(map);
//        return json;
//    }
    @GetMapping("/selectTopComment")
    @ApiOperation(value = "根据父评论id找到属于哪个一级评论",notes = "传值进来")
    public JSON selectTopComment(Long faCommentId){
        Map<Object, Object> map = new ConcurrentHashMap<>();
        String msg;
        PostComment postComment = new PostComment();
        postComment.setCommentId(faCommentId);
        List<PostComment> postComments = postCommentService.selectPostComment(postComment);
        postComment = postComments.get(0);
        while (postComment.getFaCommentId()!=null){
            PostComment postComment1 = new PostComment();
            postComment1.setCommentId(postComment.getFaCommentId());
            postComment = postCommentService.selectPostComment(postComment1).get(0);
        }
        if(postComments.size()>0) msg="200";
        else msg="500";
        map.put("code",0);
        map.put("msg",msg);
        map.put("count",1);
        map.put("data",postComment);
        JSON json = (JSON) JSON.toJSON(map);
        return json;
    }
    @PostMapping("/deleteComment")
    @ApiOperation(value = "根据id删除帖子评论",notes = "传值进来")
    public JSON deleteComment(Long commentId){
        Map<Object, Object> map = new ConcurrentHashMap<>();
        try {
            PostComment comment = new PostComment();
            comment.setCommentId(commentId);
            List<PostComment> postComments1 = postCommentService.selectPostComment(comment);
            Post post1 = new Post();
            post1.setPostId(postComments1.get(0).getPostId());
            List<Post> posts = postService.selectPost(post1);
            posts.get(0).setPostCommentNum(posts.get(0).getPostCommentNum()-1);
            postService.updatePost(posts.get(0));
            boolean b = postCommentService.deleteComment(commentId);
            PostComment postComment = new PostComment();
            postComment.setFaCommentId(commentId);
            List<PostComment> postComments = postCommentService.selectPostComment(postComment);
            for (PostComment postComment1 : postComments){
                postCommentService.deleteComment(postComment1.getCommentId());
                Post post = new Post();
                post.setPostId(postComment1.getPostId());
                post.setPostCommentNum(post.getPostCommentNum()-1);
                postService.updatePost(post);
            }
            map.put("code",0);
            map.put("msg","200");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",0);
            map.put("msg","500");
        }
        JSON json = (JSON) JSON.toJSON(map);
        return json;
    }
    @PostMapping("/LikeComment")
    @ApiOperation(value = "点赞帖子评论,调用一次点赞数+1",notes = "传值进来")
    @CacheEvict(value = "PostComment", allEntries=true)
    public JSON LikeComment(PostComment postComment){
        Map<Object, Object> map = new ConcurrentHashMap<>();
          String msg;
        try {
            List<PostComment> postComments = postCommentService.selectPostComment(postComment);
            PostComment postComment1 = postComments.get(0);
            postComment1.setCommendLikes(postComment1.getCommendLikes()+1);
            postCommentService.updateById(postComment1);
                msg="200";
        } catch (Exception e) {
            e.printStackTrace();
               msg="500";
        }
        map.put("code",0);
        map.put("msg",msg);
        JSON json = (JSON) JSON.toJSON(map);
        return json;
    }
    @PostMapping("/insertPost")
    @ApiOperation(value = "添加帖子评论接口",notes = "传值进来")
    public JSON insertComment(PostComment postComment, HttpServletRequest request) throws Exception {

        if (!SenInfoCheckUtil.cotentFilter(postComment.getCommentContent())){
            Map<Object, Object> map1 = new ConcurrentHashMap<>();
            map1.put("code",4444);
            map1.put("msg","文本敏感");
            JSON json = (JSON) JSON.toJSON(map1);
            return json;
        }
        Post post1 = new Post();
        post1.setPostId(postComment.getPostId());
        List<Post> posts1 = postService.selectPost(post1);
        Post post2 = posts1.get(0);
          GetIp getIp = new GetIp(request);
          postComment.setCommentIp(getIp.getIpAddress());
          postComment.setCommendLikes((long)0);
        if (postComment.getFaCommentId()!=null){
            PostComment postComment1 = new PostComment();
            postComment1.setCommentId(postComment.getFaCommentId());
            List<PostComment> postComments1 = postCommentService.selectPostComment(postComment1);
            if (redisTemplate.opsForHash().get("UnreadMsg",postComments1.get(0).getUserId().toString())==null){
                redisTemplate.opsForHash().put("UnreadMsg",postComments1.get(0).getUserId().toString(),1);
            }else {
                Integer unreadMsg1 = (Integer) redisTemplate.opsForHash().get("UnreadMsg", postComments1.get(0).getUserId().toString());
                redisTemplate.opsForHash().put("UnreadMsg",postComments1.get(0).getUserId().toString(),unreadMsg1+1);
            }
        }else {
            Post post = new Post();
            post.setPostId(postComment.getPostId());
            List<Post> posts = postService.selectPost(post);
            if (redisTemplate.opsForHash().get("UnreadMsg",posts.get(0).getUserId().toString())==null){
                redisTemplate.opsForHash().put("UnreadMsg",posts.get(0).getUserId().toString(),1);
            }else {
                Integer UnreadMsg2 = (Integer) redisTemplate.opsForHash().get("UnreadMsg", posts.get(0).getUserId().toString());
                redisTemplate.opsForHash().put("UnreadMsg",posts.get(0).getUserId().toString(),UnreadMsg2+1);
            }
        }
        post2.setPostCommentNum(post2.getPostCommentNum()+1);
        postService.updatePost(post2);
        Map<Object, Object> map = new ConcurrentHashMap<>();
        String msg;
        int i = postCommentService.insertComment(postComment);
        Post post = new Post();
        post.setPostId(postComment.getPostId());
        post.setPostUptime(new Date());
        postService.updatePost(post);
        if(i>0) msg="200";
        else msg="500";
        map.put("code",0);
        map.put("msg",msg);
        JSON json = (JSON) JSON.toJSON(map);
        return json;
    }
}
